<template>
  <div class="page-common-layout">
    <div class="page-common-content">
      <tool-bar @search="$refs.table.refresh(true)" @reset="
          () => {
            (this.queryParam = {}), $refs.table.refresh(true);
          }
        ">
        <template slot="toolBtn" slot-scope="scope">
          <a-button class="cu-btn-primary" icon="plus" @click="handleAdd()"
          >{{$ct("add","新增")}}
          </a-button>
        </template>
        <template slot="toolForm">
          <a-form-item :label="$ct('templateName','模板名称')">
            <a-input v-model="queryParam.name" :placeholder="$ct('templateName','模板名称')"/>
          </a-form-item>
        </template>
      </tool-bar>
      <DataGrid ref="table" :columns="columns" :url="url" :queryParam="queryParam" rowKey="uuid" :showRowSelect="false">
        <template slot="renderAction" slot-scope="{ text, record }">
          <a @click="() => handleEdit(record)">{{$ct("edit","编辑")}}</a>
          <span class="action-margin"></span>
          <a @click="() => handleSub(record)" class="action-delete">{{$ct("delete","删除")}}</a>
          <span class="action-margin"></span>
          <a @click="() => handleCopy(record)">{{$ct("copy","复制")}}</a>
        </template>
      </DataGrid>
      <edit-form ref="createModal" :visible="visible" :loading="confirmLoading" :model="mdl"
                 @cancel="handleCancel()" @ok="handleOk()"/>
    </div>
  </div>
</template>

<script>
  import {
    DataGrid
  } from "@/components";
  import {
    dataGridMixin
  } from "@/mixins/dataGridMixin";
  import editForm from "./edit.vue";

  export default {
    name: "Table",
    components: {
      DataGrid,
      editForm,
    },
    mixins: [dataGridMixin],
    data() {
      return {
        columns: [{
          title: "模板名称",
          dataIndex: "name",
          langCode: 'templateName'
        },
				{
					title: "模板分类",
					dataIndex: "type",
					langCode: 'templateClassification'
				},
				{
					title: "操作",
					dataIndex: "action",
					scopedSlots: {
						customRender: "renderAction"
					}
				}],
        visible: false,
        confirmLoading: false,
        mdl: null,
        // 查询参数
        queryParam: {},
        url: {
          getPageSet: "/system/sysGenTemplate/getPageSet",
          save: "/system/sysGenTemplate/save",
          update: "/system/sysGenTemplate/update",
          deleteBatch: "/system/sysGenTemplate/deleteBatch",
          copyTemplate: "/system/sysGenTemplate/copyTemplate"
        },
      };
    },
    methods: {
      //复制
      handleCopy(record) {
        const that = this
        let uuids = ''
        if (!record || record.length === 0) {
          this.$message.warn(this.$ct("pleaseCheckTheDataToBeCopied","请勾选需要复制的数据"), 1.5)
          return
        } else if (record && record[0]) {
          uuids = record.toString()
        } else {
          uuids = record.uuid
        }
        let value = {
          uuid: uuids,
        }
        this.$confirm({
          title: that.$ct("warning","警告"),
          content: that.$ct("executeOperationConfirm","确定要执行该操作吗"),
          okText: that.$ct("copy","复制"),
          okType: 'primary',
          cancelText: that.$ct("cancel","取消"),
          onOk() {
            // 在这里调用删除接口
            that.$post(that.url.copyTemplate, value).then((res) => {
              if (res.statusCode === 200 || res === 1) {
                that.$message.success(res.message, 1.5)
                if (that.fetch) {
                  that.fetch(that.queryParam)
                } else {
                  that.$refs.table.refresh()
                }
              } else {
                that.$message.error(res.message, 1.5)
              }
            })
          },
          onCancel() {
          }
        })

        //
        // this.$post(this.url.copyTemplate, value).then((res) => {
        //     if (res.statusCode === 200 || res === 1) {
        //         that.$message.success(res.message, 1.5)
        //         if (this.fetch) {
        //             this.fetch(this.queryParam)
        //         } else {
        //             that.$refs.table.refresh()
        //         }
        //     } else {
        //         that.$message.error(res.message, 1.5)
        //     }
        // })

      },

      // 重写编辑函数
      handleEdit: function (record, paramar) {
        if (typeof record.templateJson == 'string') {
          record.templateJson = JSON.parse(record.templateJson)
        }
        if (!record) {
          this.$message.warn(this.$ct("pleaseSelectTheDataToBeOperated","请选中需要操作的数据"), 1.5)
        } else {
          this.visible = true
          this.mdl = {...record, ...paramar}
          if (this.$refs.createModal.edit) this.$refs.createModal.edit(this.mdl)
        }
        this.$refs.createModal.title = this.$ct("edit","编辑")
      },
      // 提交表单
      handleOk: function (paramar) {
        const form = this.$refs.createModal.form
        this.$refs.createModal.panes.forEach((item, index) => {
          this.$refs.createModal.panes[index].tabData = this.$refs.createModal.$refs[`monaco${item.tabName}`][0].getValue()
        })
        let templateJson = JSON.stringify(this.$refs.createModal.panes)
        this.confirmLoading = true
        let url = ''
        form.validateFields((errors, values) => {
          // if(type)fromValue.isRelease = type
          let fromValue = {
            ...values,
            ...paramar,
            templateJson: templateJson
          }
          if (!errors) {
            if (values.uuid) {
              // 修改 e.g.
              url = this.url.update
            } else {
              url = this.url.save
            }
            this.$post(url, fromValue).then((res) => {
              if (res.statusCode === 200 || res === 1) {
                this.$refs.table.rows = null
                this.$refs.table.rowKeys = null
                this.visible = false
                // 刷新表格
                if (this.fetch) {
                  this.fetch(this.queryParam)
                } else {
                  this.$refs.table.refresh()
                }
                this.$message.success(res.message || this.$ct("operationSucceeded","操作成功"), 1.5)
              } else {
                this.$message.success(res.message || this.$ct("operationFailed","操作失败"), 1.5)
              }
            }).finally(() => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
    },
  };
</script>

<style scoped lang="less">
  .table {
    background-color: @base-bg-color;
    padding: 24px;
  }
</style>





